#coding=utf-8
import threading
from datetime import datetime
from com.jumang.content.CronRule import CronRule
import time
import sys
class BaseCheck(threading.Thread):
  def __init__(self,fullString,event):
    threading.Thread.__init__(self)
    self.lockObj=threading.Condition()
    self.isExit=False
    self.source=fullString
    self.lastTime=datetime.now()
    self.cronRule=CronRule()
    self.appname=unicode(fullString.split('||')[1].strip(),'utf-8')
    try:
      self.cronRule.setCron(fullString.split('||')[2].strip())
    except:
      print 'Cron table formate is error!'
    self.event=event
    self.force=False
    
  def getName(self):
    return self.appname
  def check(self):
    pass
  def terminate(self):
    self.lockObj.acquire()
    self.isExit=True
    self.lockObj.notify()
    self.lockObj.release()
    
  def notifyCheck(self):
#    print 'notify -------'
    self.lockObj.acquire()
    self.lockObj.notify()
    self.lockObj.release()
    
  def forceCheck(self):
#    print 'force -------'
    self.lockObj.acquire()
    self.force=True
    self.lockObj.notify()
    self.lockObj.release()

    
    
  def run(self):
    while not self.isExit:
      self.lockObj.acquire()
      self.lockObj.wait()
      self.lockObj.release()
#      print 'run loop ------'
      if self.isExit:
        break
      if self.cronRule.isOnTime() or self.force:
        self.force=False
        try:
          self.lastTime=datetime.now()
          errorNo,errorInfo=self.check()
        except:
          errorNo=-999
          errorInfo=sys.exc_info()[0]
        if errorNo!=0:
          self.event.notifyEvent(self,errorNo,errorInfo)
      